﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using ST.Torch.WebApi.Core.Entities.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace ST.Torch.WebApi.Application.System.Services
{
	public class SYSUserService : ISYSUserService, ITransient
	{
		private readonly IRepository<SYSUser> _SYSUserRepository;
		public SYSUserService(IRepository<SYSUser> SYSUserRepository)
		{
			_SYSUserRepository = SYSUserRepository;
		}

		#region curd
		public  void Insert(SYSUser entity)
		{
			 _SYSUserRepository.Insert(entity);
		}
		public async Task InsertAsync(SYSUser entity, string[] Includes=null)
		{
				await _SYSUserRepository.InsertAsync(entity);
			
		}
		public void Update(SYSUser entity, string[] Includes = null)
		{
			if (Includes == null)
				 _SYSUserRepository.Update(entity);
			else
				 _SYSUserRepository.UpdateInclude(entity, Includes);
		
		}
		public async Task UpdateAsync(SYSUser entity, string[] Includes = null)
		{ 
			if (Includes == null)
				await _SYSUserRepository.UpdateAsync(entity);
			else
				await _SYSUserRepository.UpdateIncludeAsync(entity, Includes);
		}
		public void Delete(SYSUser entity, bool IsSoft=false)
		{
			if (IsSoft)
			{
				var fields= entity.Delete();
				Update(entity, fields);
			}
			else
			{
				_SYSUserRepository.Delete(entity);
			}
				
		}
		public async Task DeleteAsync(SYSUser entity, bool IsSoft = false)
		{
			if (IsSoft)
			{
				var fields = entity.Delete();
				await UpdateAsync(entity, fields);
			}
			else
			{
				await _SYSUserRepository.DeleteAsync(entity);
			}

		}
		
        public bool Any(Expression<Func<SYSUser, bool>> predicate)
        {
            return  _SYSUserRepository.Any(predicate);
        }
        public async Task<bool> AnyAsync(Expression<Func<SYSUser, bool>> predicate)
        {
                return await _SYSUserRepository.AnyAsync(predicate);
        }
        public SYSUser Single(Expression<Func<SYSUser, bool>> predicate, bool tracking = false)
        {
            return _SYSUserRepository.SingleOrDefault(predicate, tracking);
        }
        public async Task<SYSUser> SingleAsync(Expression<Func<SYSUser, bool>> predicate, bool IsIncludeRole = false, bool tracking = false)
		{
			if (!IsIncludeRole)
			{
                return await _SYSUserRepository.SingleOrDefaultAsync(predicate, tracking);
            }
			else
			{
                return await _SYSUserRepository.AsQueryable(tracking)
					.Include(u => u.Roles)
					.ThenInclude(u=>u.Menus)
					.SingleOrDefaultAsync(predicate);
            }


        }
		public  List<SYSUser> Query(Expression<Func<SYSUser, bool>> predicate, bool tracking = false)
		{
            return  _SYSUserRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToList();
         
        }
		public async Task<List<SYSUser>> QueryAsync(Expression<Func<SYSUser, bool>> predicate,bool tracking=false)
		{
            return await _SYSUserRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToListAsync();

 
		}

		public PagedList<SYSUser> QueryPage(Expression<Func<SYSUser, bool>> predicate, PageReq page, bool tracking = false)
		{
			return _SYSUserRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToPagedList(page.page, page.limit);
		}
		public  async Task<PagedList<SYSUser>> QueryPageAsync(Expression<Func<SYSUser, bool>> predicate, PageReq page, bool tracking = false)
		{
			return await _SYSUserRepository.Where(predicate,tracking).OrderByDescending(u => u.CreateTime).ToPagedListAsync(page.page, page.limit);
		}




		#endregion

	}
}
